﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilLibrary;
using DAL;

namespace Entity
{
    public class BizWorkflow : BaseBizEntity
    {
        public BizWorkflow(int pTaskId)
        {
            this.taskId = pTaskId;
        }
        // 全部节点的列表
        private List<BizWorkflowSequeuece> allNodes = new List<BizWorkflowSequeuece>();
        // 还没有走到的节点的列表
        private Queue<BizWorkflowSequeuece> nodes = new Queue<BizWorkflowSequeuece>();

        public Queue<BizWorkflowSequeuece> Nodes
        {
            get { return nodes; }
            set { nodes = value; }
        }

        public BizWorkflowSequeuece activeNode = null;

        private int taskId;

        public int TaskId
        {
            get { return taskId; }
            set { taskId = value; }
        }

        public bool SetNextActiveNode()
        {
            if (nodes.Count == 0)
                return false;

            activeNode = nodes.Dequeue();
            activeNode.PassedFlg = true;
            activeNode.Update();
            this.Update();

            return true;
        }

        public void Add(BizWorkflowSequeuece pNode)
        {
            if (this.Id == 0)
            {
                throw new Exception("工作流实例为空，不能添加节点");
            }

            pNode.WfId = this.Id;
            this.nodes.Enqueue(pNode);
            this.allNodes.Add(pNode);
            // 如果stepno没有赋值，取最大数+1作为no值
            if (pNode.StepNo == 0)
                pNode.StepNo = this.allNodes.Count + 1;
            pNode.Create();
        }

        //public void Delete(int pStepNo)
        //{
        //    var node = allNodes.Where(p => p.StepNo == pStepNo).FirstOrDefault();
        //    if (node != null)
        //    {
        //        allNodes.Remove(node);
        //        nodes.ToList().Remove(node);
        //        node.Delete();
        //    }
        //}

        public int GetEmpIDByStepNo(int pStepNo)
        {
            int empID = 0;
            foreach (BizWorkflowSequeuece node in this.nodes)
            {
                if (node.StepNo == pStepNo)
                {
                    empID = node.EmpID;
                }
            }

            return empID;
        }

        public String getStepNameByStepNo(int pStepNo)
        {
            String ret = String.Empty;
            foreach (BizWorkflowSequeuece node in this.nodes)
            {
                if (node.StepNo == pStepNo)
                {
                    ret = node.StepName;
                }
            }

            return ret;
        }

        public int NodeCount
        {
            get
            {
                return this.nodes.Count;
            }
        }

        /// <summary>
        /// 直接到达工作流的结尾
        /// </summary>
        public void Over()
        {
            activeNode = this.nodes.Last();
            this.nodes.Clear();
        }

        public override void SetEntityType()
        {
            this.dataEntityType = typeof(DAL.workflow);
        }

        public static BizWorkflow GetWorkflowByTaskID(int pTaskId)
        {
            Model1Container container = DbUtil.GetDbContainer();
            // 获取工作流实例
            workflow wf = container.workflow.Where(p => p.TaskId == pTaskId).FirstOrDefault();
            BizWorkflow bizWF = new BizWorkflow(pTaskId);
            DbUtil.DataEntityToBizObjTrans(wf, bizWF);
            // 获取工作流还未走到的节点
            List<workflowsequeuece> query = container.workflowsequeuece.Where(p => p.WFID == wf.Id).OrderBy(p => p.StepID).ToList();

            foreach (workflowsequeuece item in query)
            {
                BizWorkflowSequeuece wfNode = new BizWorkflowSequeuece();
                DbUtil.DataEntityToBizObjTrans(item, wfNode);
                bizWF.allNodes.Add(wfNode);

                if (!item.PassedFlg.Value)
                    bizWF.nodes.Enqueue(wfNode);
            }

            bizWF.activeNode = bizWF.nodes.FirstOrDefault();
            return bizWF;
        }
    }
}
